package NWish
{
	import away3d.containers.Scene3D;
	import away3d.events.MouseEvent3D;
	
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	import org.aswing.JToggleButton;
	
	import NWish.Interface.ILogger;
	
	
	public class Selector
	{
		private var _parent;
		
		//
		private var _selectToggleButton:JToggleButton;

		
		public function Selector(parent)
		{
			//
			_parent = parent;
			
			
			//
			_parent.addEventListener(NWEvent.NEW_SCENE,onNewScene);
			_parent.addEventListener(NWEvent.LOADED_SCENE,onLoadedScene);
			_parent.addEventListener(NWEvent.CREATE_OBJECT,onCreateObject);
			//_parent.addEventListener(NWEvent.DELETE_OBJECT,onDeleteObject);
			
		}
		
		public function init()
		{
			//
			var menuBar:MenuBar = _parent.getPlugin('MenuBar') as MenuBar;
			
			_selectToggleButton = new JToggleButton("pick up");
			_selectToggleButton.y = 2;
			_selectToggleButton.setToolTipText('Hot key 1 is active. Hot key Q is set to Object Explorer. Hot key W is show in Properties Explorer. Hot key E is multi apply');//, hot key e is cope name
			menuBar.addButton(_selectToggleButton,'Group0',Keyboard.NUMBER_1);//1 key code is 49*/
			
			//
			_parent.stage.addEventListener(KeyboardEvent.KEY_UP, 	onKeyUp);
		}
		
		public function get isSelected()
		{
			return _selectToggleButton.isSelected();
		}
		
		public function get scene():Scene3D
		{
			var away3d = _parent.getPlugin('Away3d');
			return away3d.view.scene;
		}
		
		private function onNewScene(e:NWEvent)
		{
			scene.addEventListener(MouseEvent3D.CLICK,onClick);
		}
		private function onLoadedScene(e:NWEvent)
		{
			scene.addEventListener(MouseEvent3D.CLICK,onClick);
		}
		
		private function onCreateObject(e:NWEvent)
		{
			if(e.objects is Array)
			{
				for each (var o in e.objects)
				{
					//o.addEventListener(MouseEvent3D.CLICK,onClick);
					if(o.hasOwnProperty('mouseEnabled'))
						o.mouseEnabled = true;
				}
			}
			else
			{
				//e.objects.addEventListener(MouseEvent3D.CLICK,onClick);
				if(e.objects.hasOwnProperty('mouseEnabled'))
					e.objects.mouseEnabled = true;
			}
		}
		
		/*private function onDeleteObject(e:NWEvent)
		{
			if(e.objects is Array)
			{
				for each (var o in e.objects)
				{
					o.removeEventListener(MouseEvent3D.CLICK,onClick);
				}
			}
			else
			{
				e.objects.removeEventListener(MouseEvent3D.CLICK,onClick);
			}
		}*/
		
		//
		private var _selectObject3ds:Array = [];
		private function onClick(e:MouseEvent3D)
		{
			if(isSelected == false)
				return;
			
			if(e.ctrlKey)
			{
				var find = -1;
				if(_selectObject3ds.length > 0)
					find = _selectObject3ds.indexOf(e.object);
				
				if(find == -1)
				{
					_selectObject3ds.push(e.object);
					ILogger.log('add pick up!',e.object);
				}
				else
				{
					_selectObject3ds.splice(find,1);
					ILogger.log('remove pick up!',e.object);
				}
				
				_parent.dispatchEvent(new NWEvent(NWEvent.SELSCT_OBJECT,_selectObject3ds));
			}
			else
			{
				_selectObject3ds = [];
				_selectObject3ds.push(e.object);
				
				_parent.dispatchEvent(new NWEvent(NWEvent.SELSCT_OBJECT,e.object));
				ILogger.log('pick up!',e.object);
			}
			
		}
		
		
		//
		static private var _q = false;//key code 81
		static private var _w = false;//key code 87
		private function onKeyUp(e:KeyboardEvent) : void
		{
			if(e.target != _parent.stage)
				return;
			
			if(isSelected == false  || e.ctrlKey || e.shiftKey)
				return;
			
			if(_selectObject3ds.length == 0)
				return;
			
			if(e.keyCode == Keyboard.Q)
			{
				_parent.getPlugin('ObjectExplorer').setObjects(_selectObject3ds);
			}
			else if(e.keyCode == Keyboard.W)
			{
				_parent.dispatchEvent(new NWEvent(NWEvent.SELSCT_OBJECT,_selectObject3ds))
			}
			else if(e.keyCode == Keyboard.E)
			{
				_parent.dispatchEvent(new NWEvent(NWEvent.MULTI_APPLY,_selectObject3ds))
			}
			/*else if(e.keyCode == 69)//e key code 87
			{
				var str = '';
				
				for each(var obj in _selectObject3ds)
				{
					if(str.length)
						str += ', ';
					
					str += obj.name;
				}
				
				Clipboard.generalClipboard.clear();
				Clipboard.generalClipboard.setData(ClipboardFormats.TEXT_FORMAT, str);
			}*/
		}
	}
}








